package org.gitos.cloud.comet.mq;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.gitos.cloud.comet.logic.recMQLogic.SndContentEvent;
import org.gitos.cloud.domain.comm.Constant;
import org.gitos.cloud.domain.mq.MqMsg;
import org.gitos.cloud.domain.tcp.Bucket;
import org.gitos.cloud.util.HexUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.handler.annotation.Payload;

import com.alibaba.fastjson.JSON;

/**
 * Created by shizhengchu on 15/12/10. 每个comet接收comet.exchange的消息,
 * nodeId作为queue name, routekey也是nodeId
 */
@Configuration
@RabbitListener(bindings = @QueueBinding(value = @Queue(value = "${"
        + Constant.NODE_ID + "}", durable = "true"), exchange = @Exchange(value = Constant.COMET_EXCHANGE, durable = "true"), key = "${"
        + Constant.NODE_ID + "}"))
public class RecieveManager {

    private static final Logger log = LoggerFactory
            .getLogger(RecieveManager.class);

    private ExecutorService fixThreadPool = Executors
            .newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);

    int count = 0;

    @RabbitHandler
    public void onMessage(@Payload String message) {
        log.info(message.toString());
        try {
            MqMsg msg = JSON.parseObject(message.toString(), MqMsg.class);
            dispatch(msg);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    private void dispatch(MqMsg msg) {
        Bucket bucket = msg.getBucket();
        String deviceId = HexUtil.encodeHexStr(bucket.getDeviceId().getValue());
        String content = new String(bucket.getContent());
//        switch (bucket.getCmd()) {
//            case Constant.CMD_CHANGE_LED_STATE:
//                System.out.println("请求" + deviceId + "改变状态：" + content);
//                break;
//            case Constant.CMD_RESET_LED_STATE:
//                System.out.println("请求重置" + deviceId + "的状态");
//                break;
//            case Constant.CMD_QUERY_LED_STATE:
//                System.out.println("请求获取" + deviceId + "的状态");
//                break;
//            case Constant.CMD_UNKNOW4:
//            case Constant.CMD_SET_LED_ON_OFF_TIME:
//                System.out.println("开关灯时间为：" + content);
//                break;
//            case Constant.CMD_APPLY_OTA:
//                System.out.println("请求" + deviceId + "ota");
//                break;
//            case Constant.CMD_QUERY_OTA_PROGRESS:
//                System.out.println("请求查看" + deviceId + "ota进度");
//                break;
//            case Constant.CMD_UNKONW9:
//            default:
//                break;
//        }
        SndContentEvent sc = new SndContentEvent(msg);
        fixThreadPool.execute(sc);
    }

}
